﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AteneoLibri
{
    public partial class MarketingUnsubscribe : System.Web.UI.Page
    {

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                if (User.Identity.Name.Equals(System.Configuration.ConfigurationManager.AppSettings["AdminName"]))
                {
                    Page.MasterPageFile = "~/BackEnd/BackEndMasterPage.Master";
                }
                else
                {
                    Page.MasterPageFile = "~/Account/NewAccountMasterPage.Master";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUnsubscribe_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                {
                    cvUser.IsValid = service.CheckExistingRegistration(txtEmail.Text.ToLower().Trim());

                    if (Page.IsValid)
                    {
                        AteneoLibriBusinessLogic.Entities.User user = service.getUserByEmail(txtEmail.Text.Trim());

                        if (user != null)
                        {
                            user.MarketingNewsletter = false;

                            bool result = service.updateUser(user);
                            if (result)
                            {
                                // open confirmation panel
                                pnlUnsubscribeCompleted.Visible = true;
                                pnlEmail.Visible = false;
                            }
                            else
                            {
                                pnlUnsubscribeError.Visible = true;
                                pnlEmail.Visible = false;
                            }
                        }
                        
                    }
                }
            }
        }
    }
}